﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xinkui.WebModel.Cache.View;
using Xinkui.Common;
using Xinkui.DataAccess.AutoGenerator;
using Xinkui.Entity.AutoGenerator;

namespace Xinkui.WebModel.Cache {
    public static class CountryCache {

        public static CountryView Get(int countryId) {
            Dictionary<int, CountryView> dict = List();
            CountryView outView = null;
            if (dict != null) {
                dict.TryGetValue(countryId, out outView);
            }
            return outView;
        }

        public static Dictionary<int, CountryView> List() {
            return CacheProxy.Get<Dictionary<int, CountryView>>(SystemConst.CacheConst.GetCountryDictKey(), ListFromDb);
        }


        public static Dictionary<int, CountryView> ListFromDb() {
            List<CommonCountry_Entity> datas = Singleton<CommonCountryDataAccess>.Instance.CommonCountry_All();
            if (datas != null && datas.Count > 0) {
                return datas.ToDictionary(u => u.CountryID, u => CountryView.Convert(u));
            }
            else {
                return null;
            }
        }
    }
}
